<?php

function select ($params)
{
    
    $name = null;
    $values = null;
    $options = null;
    $selected = array();
    $output = null;
    $on_select = '';
    $first_false = false;
    $on_first_false = '';
    $extra = '';
    $object_value = false;
    $object_key = false;
    $size = false;
    
    foreach($params as $_key => $_val) {
        switch($_key) {
            case 'object_value':
            case 'object_key':
                $$_key = str_replace('$', '$value->', (string)$_val);
                break;
            case 'name':
                $$_key = (string)$_val;
                break;
            case 'first_as_false':
                $$_key = $_val;
                break;
            case 'options':
                if (is_a($_val, 'ActiveRecordCollection') || is_a($_val, 'Collection')) {
                  $$_key = $_val;
                } else {
                  $$_key = (array)$_val;
                }
                break;
                
            case 'values':
            case 'output':
                if (is_a($_val, 'Collection')) {
                  $$_key = array_values($_val->to_array());
                } else {
                  $$_key = array_values((array)$_val);
                }
                break;
                
            case 'selected':
                $$_key = array_map('strval', array_values((array)$_val));
                break;
                
            case 'on_select':
                break;
            case 'first_false':
                $$_key = $_val;;
                break;
                
            default:
                if(!is_array($_val)) {
                    $extra .= ' '.$_key.'="'.($_val).'"';
                } else {
                    $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }
    
    
    
    
    if (!isset($options) && !isset($values))
        return ''; /* raise error here? */
        
    $_html_result = '';
    
    
    
    if ($first_false !== false) {
        $first_false_label = $first_false === true ?  '' : $first_false;
        
        $_html_result .= smarty_function_html_options_optoutput(false, $first_false_label, $selected, $object_value);
        if ($params['on_first_false'] === true) {
          $on_select = " onChange=\"{$params['on_select']}\"";
        } else {
          $on_select = " onChange=\"if (this.value != false) { {$params['on_select']} } else { {$params['on_first_false']} }\"";
        }
        
    } else {
        $on_select = " onChange=\"{$params['on_select']}\"";
        
    }
    if (isset($first_as_false)) {
        $_html_result .= smarty_function_html_options_optoutput(false, $first_as_false, $selected);
    }
    if (isset($options)) {
        if ($params['size'] === true) {
		    	$size = count ($options);
				} else {
		    	$size = intval($params['size']);
				}
        foreach ($options as $_key => $_val) {
            $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $object_value, $object_key);
        }
            
    } else {
        if ($params['size'] === true) {
		    	$size = count ($values);
				} else {
		    	$size = intval($params['size']);
				}
        
        foreach ($values as $_i=>$_key) {
            $_val = isset($output[$_i]) ? $output[$_i] : '';
            $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $object_value, $object_key);
        }
        
    }
    
    
    if(!empty($name)) {
        $_html_result = '<select' . $on_select . ' id="' . $name . '" name="' . $name . '" size="' . $size . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
    }
    //pc($_html_result, true);
    return $_html_result;

}
function spt_function_object_value () {

}


function smarty_function_html_options_optoutput($key, $value, $selected, $object_value = false, $object_key = false, $prefix = '') {
    if(!is_array($value)) {
        if ($object_value) {
          $code = "\$_val = \"{$object_value}\";";
          eval ($code);
        } else {
          $_val = $value;
        }
        if ($object_key) {
          $code = "\$_key = \"{$object_key}\";";
          eval ($code);
        } else {
          $_key = $key;
        }
        
        $_html_result = '<option label="' . ($_val) . '" value="' .
            ($_key) . '"';
        if (in_array((string)$_key, $selected))
            $_html_result .= ' selected="selected"';
        $_html_result .= '>' . $prefix . ($_val) . '</option>' . "\n";
        
        if(is_object($value) && is_array($value->children)) {
          $children = $value->children;
          $prefix .= '&nbsp; &nbsp; ';
          foreach ($children as $_key => $_val) {
            $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $object_value, $object_key, $prefix);
          }
        } 
        
    } else {
        $_html_result = smarty_function_html_options_optgroup($key, $value, $selected);
    }
    return $_html_result;
}

function smarty_function_html_options_optgroup($key, $values, $selected) {
    $optgroup_html = '<optgroup label="' . ($key) . '">' . "\n";
    foreach ($values as $key => $value) {
        $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected);
    }
    $optgroup_html .= "</optgroup>\n";
    return $optgroup_html;
}

/* vim: set expandtab: */

?>
